#include "StdAfx.h"
#include "StdConsole.h"
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <fstream>

StdConsole::StdConsole(void) : GameObject(ClassStdConsole)
{
}

StdConsole::~StdConsole(void)
{
}

HRESULT StdConsole::OnInitSingleton(void)
{
	if (AllocConsole() == false)
		return E_UNEXPECTED;
	HANDLE _hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	if (_hConsoleOutput == INVALID_HANDLE_VALUE)
		return E_UNEXPECTED;
	int SystemOutput = _open_osfhandle(intptr_t(_hConsoleOutput), _O_TEXT);
	if (SystemOutput == -1)
		return E_UNEXPECTED;
	FILE *COutputHandle = _fdopen(SystemOutput, "w" );
	*stdout = *COutputHandle;
	setvbuf(stdout, NULL, _IONBF, 0);
	return S_OK;
}

void StdConsole::OnUninitSingleton(void)
{
	FreeConsole();
}
